
 /*
- * Copyright (C) 2006-2008 the VideoLAN team
+ * Copyright (C) 2006-2009 the VideoLAN team
  *
  * This file is part of VLMa.
  *
@@ -21,8 +21,6 @@
 package org.videolan.vlma.monitor;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.Socket;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -32,6 +30,7 @@ import org.apache.log4j.Logger;
 import org.videolan.vlma.dao.VLMaDao;
 import org.videolan.vlma.model.Server;
 import org.videolan.vlma.notifier.Notifier;
+import org.videolan.vlma.order.sender.TelnetConnection;
 
 /**
  * This class is the daemon which monitores the servers' state. It periodically
@@ -44,7 +43,6 @@ import org.videolan.vlma.notifier.Notifier;
 public class ServerStateMonitor extends Monitor {
 
     private static final Logger logger = Logger.getLogger(ServerStateMonitor.class);
-    public static final int SOCKET_TIMEOUT = 5000;
 
     private Configuration configuration;
     private VLMaDao vlmaDao;
@@ -86,14 +84,30 @@ public class ServerStateMonitor extends Monitor {
     public boolean checkVLC(Server server) {
         synchronized(server) {
             boolean formerState = server.isUp();
+            TelnetConnection conn = new TelnetConnection();
+            int port = configuration.getInt("vlc.telnet.port");
             try {
-                Socket socket = new Socket();
-                socket.connect(new InetSocketAddress(server.getIp(), configuration.getInt("vlc.telnet.port")), SOCKET_TIMEOUT);
-                socket.close();
-                server.setUp(true);
-                logger.trace("VLC of " + server.getName() + " is reachable");
+                conn.connect(server.getIp(), port);
+                conn.println(configuration.getString("vlc.telnet.password"));
+                try {
+                    conn.waitUntilReady();
+                    conn.resetIn();
+                    conn.println("help");
+                    conn.waitUntilReady();
+                    int c = conn.read();
+                    if (c >= 0) {
+                        server.setUp(true);
+                        logger.trace("Telnet interface of " + server.getName() + " is OK");
+                    } else {
+                        logger.info("Telnet interface of " + server.getName() + " does not reply");
+                        server.setUp(false);
+                    }
+                } catch (IOException e) {
+                    logger.info("Telnet interface of " + server.getName() + " does not give any prompt");
+                    server.setUp(false);
+                }
             } catch (IOException e) {
-                logger.info("Unable to contact VLC server of " + server.getName() + " through telnet interface");
+                logger.info("Unable to contact telnet interface of " + server.getName());
                 server.setUp(false);
             }
             boolean newState = server.isUp();
diff --git a/vlma-core/src/main/java/org/videolan/vlma/order/sender/TelnetConnection.java b/vlma-core/src/main/java/org/videolan/vlma/order/sender/TelnetConnection.java
index acf57fc..3bf7305 100644
--- a/vlma-core/src/main/java/org/videolan/vlma/order/sender/TelnetConnection.java
+++ b/vlma-core/src/main/java/org/videolan/vlma/order/sender/TelnetConnection.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 the VideoLAN team
+ * Copyright (C) 2008-2009 the VideoLAN team
  *
  * This file is part of VLMa.
  *
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 
 /**
@@ -39,12 +40,19 @@ public class TelnetConnection {
     private static final int TELNET_WAIT_INTERVAL = 100;
     private static final int TELNET_WAIT_MAX = 5000;
 
+    private static final int DEFAULT_SOCKET_TIMEOUT = 5000;
+
     private Socket socket;
     private PrintWriter out;
     private BufferedReader in;
 
     public void connect(InetAddress address, int port) throws IOException {
-        socket = new Socket(address, port);
+        connect(address, port, DEFAULT_SOCKET_TIMEOUT);
+    }
+
+    public void connect(InetAddress address, int port, int timeout) throws IOException {
+        socket = new Socket();
+        socket.connect(new InetSocketAddress(address, port), timeout);
         out = new PrintWriter(socket.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     }


    Previous message: [vlma-devel] commit: Do not update the state of the program here. (Adrien Grand )
    Next message: [vlma-devel] commit: Check the state of the VLC instances as soon as possible. ( Adrien Grand )
    Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

More information about the vlma-devel mailing list
